package easy;

public class Solution_1360 {

    private int[] m = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    public int daysBetweenDates(String date1, String date2) {
        int[] data_1 = str2Date(date1);
        int[] data_2 = str2Date(date2);
        if (data_1[0] > data_2[0] || (data_1[0] == data_2[0] && data_1[1] > data_2[1]) || (data_1[0] == data_2[0]
            && data_1[1] == data_2[1] && data_1[2] > data_2[2])) {
            return daysBetweenDates(date2, date1);
        }
        int count = 0;
        for (int i = data_1[0]; i < data_2[0]; i++) {
            if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) {
                count += 366;
            } else {
                count += 365;
            }
        }
        count += (dayOfYear(data_2) - dayOfYear(data_1));
        return count;
    }

    private int[] str2Date(String source) {
        return new int[]{Integer.parseInt(source.substring(0, 4)), Integer.parseInt(source.substring(5, 7)),
            Integer.parseInt(source.substring(8, 10))};
    }

    private int dayOfYear(int[] date) {
        int count = 0;
        for (int i = 0; i < date[1] - 1; i++) {
            count += m[i];
        }
        count += date[2];
        if (date[1] > 2 && (date[0] % 4 == 0 && date[0] % 100 != 0) || date[0] % 400 == 0) {
            count++;
        }
        return count;
    }

    public static void main(String[] args) {
        Solution_1360 model = new Solution_1360();
        System.out.println(model.daysBetweenDates("2019-06-29", "2019-06-30"));
        System.out.println(model.daysBetweenDates("2020-01-15", "2019-12-31"));
    }
}
